// pages/finance/withdraw/page3/page3.js
var util = require('../../../../utils/util.js');
var config = require('../../../../config/config.js');
var http = require('../../../../utils/http.js');
Page({
  watchInput(e) {
    var that = this;
    var inputValue = e.detail.value;
    that.setData({
      "codeArr": inputValue,
    })
  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true,
    })
  },
  //去微信提现步骤4
  toStep4() {
    let that = this;
    this.setData({
      "withdrawForm.mobile": that.data.user.phone
    })
    this.setData({
      "withdrawForm.code": this.data.codeArr
    })
    if (this.data.withdrawForm.code.length !== 4) {
      wx.showToast({
        title: '请输入完整的验证码',
        icon: 'none'
      })
    }
    this.withdraw();

  },
  //点击获取短信验证码
  getCode() {
    if (!this.data.user.phone){
      wx.showToast({
        title: '检测到你还没有填写手机号等信息,请去pc上填写信息',
        icon: 'none'
      });
      return false;
    }
    var time = this.data.codeObj.time;
    let that = this;
    this.sendMessage();
    
  },
  //发送请求获取短信验证码
  sendMessage() {
    let that = this;
    http.POST({
      url: 'api/publics/sendSms',
      data: {
        mobile: that.data.user.phone,
        template_id: config.template_id
      },
      success: function(res) {
        if (res.data.error !== 0) {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        } else {
          that.data.codeObj.timer = setInterval(function () {
            that.setData({
              "codeObj.message": time + "s后重新获取"
            });
            that.setData({
              "codeObj.isActiveSend": false
            });
            time--;
            if (time === 0) {
              clearInterval(that.data.codeObj.timer);
              that.setData({
                "codeObj.isActiveSend": true
              });
              that.setData({
                "codeObj.message": "获取验证码"
              });
            }
          }, 1000)
        }
      }
    })
  },
  //发送提现请求
  withdraw() {
    if (!this.data.user.phone) {
      wx.showToast({
        title: '检测到你还没有填写手机号等信息,请去pc上填写信息',
        icon: 'none'
      });
      return false;
    }
    let that = this;
    http.POST({
      url: 'api/user/withdrawMoney',
      data: that.data.withdrawForm,
      success: function(res) {
        if (res.data.error === 0) {
          wx.navigateTo({
            url: '../step4/step4',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    isFocus: true, //聚焦
    codeLength: 4,
    codeObj: {
      isActiveSend: true,
      timer: null,
      message: '获取验证码',
      time: 60,
    },
    user: {},
    withdrawForm: {},
    codeArr: '',
    focusArr: [{
        code: '',
        isFocus: true
      },
      {
        code: '',
        isFocus: false
      }, {
        code: '',
        isFocus: false
      },
      {
        code: '',
        isFocus: false
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    wx.getStorage({
      key: 'withdrawForm',
      success: function (res) {
        that.setData({
          "withdrawForm": res.data
        })
      },
    });
    this.setData({
      "user": wx.getStorageSync('user')
    })
    this.setData({
      "user.lastFourMobile": util.getLastFourNumber(this.data.user.phone)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})